package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	v1 "RoccBlog/repository/v1"
	"RoccBlog/service"
	"github.com/gin-gonic/gin"
)

// ProjectItemService 项目管理业务层
type ProjectItemService struct {
	service.BaseService
	//接口封装业务层CURD
	service.BaseOperationService
	//依赖的持久层
	Repository v1.ProjectItemRepository
}

// 将ProjectItemService注册到全局
func init() {
	global.RegisterService(&ProjectItemService{
		Repository: v1.ProjectItemRepository{},
	})
}

// Add 添加项目管理
func (s *ProjectItemService) Add(ctx *gin.Context, data model.ProjectItem) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	//设置添加记录人未当前登录用户
	data.CreateBy = userId

	return s.Repository.Create(data)
}

// Remove 删除项目管理
func (s *ProjectItemService) Remove(ctx *gin.Context, id int) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Delete(id, userId)
}

// Modify 修改项目管理
func (s *ProjectItemService) Modify(ctx *gin.Context, data model.ProjectItem) error {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return err
	}
	return s.Repository.Update(data, userId)
}

// FindById 查找ById项目管理
func (s *ProjectItemService) FindById(ctx *gin.Context, id int) (model.ProjectItem, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.ProjectItem{}, err
	}
	return s.Repository.SelectById(id, userId)
}

// Find 查找列表项目管理
func (s *ProjectItemService) Find(ctx *gin.Context, params req_resp.ProjectItemReq) (any, error) {
	userId, err := s.GetLoginUserId(ctx)
	if err != nil {
		return model.ProjectItem{}, err
	}
	size, num := s.GetPage(ctx)
	return s.Repository.Select(params, num, size, userId)
}
